package com.jtjrenren.android.taxi.driver.base;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.baidu.mapapi.SDKInitializer;
import com.igexin.sdk.PushManager;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.TtsManger;
import com.jtjrenren.android.taxi.driver.manger.service.MainService;
import com.jtjrenren.android.taxi.driver.utils.UIHelper;
import com.library.utils.AppException;
import com.library.utils.LogUtil;
import com.liulishuo.filedownloader.FileDownloader;
import com.library.utils.AppUtils;

/**
 * Created by Administrator on 2016/2/26.
 */
public class BaseApplication extends Application {

    private static BaseApplication instance;

    public MainService mainService;

    public static BaseApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();

        addListeners();
    }


    private void init() {

        if (AppUtils.isProcessRunning(getApplicationContext())) {
            LogUtil.log("app", "全局初始化！");

            instance = this;

            //异常处理
            AppException.getInstance().init(getApplicationContext());

            //业务处理器初始化
            AppManger.getInstance().init(getApplicationContext());

            //百度地图初始化
            SDKInitializer.initialize(getApplicationContext());

            //tts语音初始化
            TtsManger.getInstance().init(getApplicationContext());
            TtsManger.getInstance().start();

            //imageloader初始化
            UIHelper.initImageLoader(getApplicationContext());

            //绑定主服务
            bindMainService();

//            LeakCanary.install(this);

        }
        //下载框架的缓存
        FileDownloader.init(getApplicationContext());

        LogUtil.log("push", "推送初始化_application");
        PushManager.getInstance().initialize(this.getApplicationContext());
    }

    /**
     * 绑定服务
     */
    public void bindMainService() {
        if (mainService != null && AppUtils.isServiceRunning(getApplicationContext(), MainService.class.getName())) {
            LogUtil.log_d(BaseApplication.class, "主服务已经启动，无须再启动");
            return;
        }

        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                LogUtil.log_d(BaseApplication.class, "主服务启动，绑定成功！");
                mainService = ((MainService.LocalBinder) iBinder).getMainService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
            }
        };

        Intent intent = new Intent(getApplicationContext(), MainService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }

    private int activityCount = 0;//activity的count数
    private boolean isForeground = true;//是否在前台
    private void addListeners() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                if (!isForeground) {
                    LogUtil.log("push","程序从后台切换回前台，重新初始化个推");
                    PushManager.getInstance().initialize(getApplicationContext());
                }
                isForeground = true;
                activityCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                activityCount--;
                if (0 == activityCount) {
                    isForeground = false;
                }
                LogUtil.log("app", "当前程序是否在前台：" + isForeground);
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

}
